package com.vrv.im.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vrv.im.domain.ProjectBean;
import com.vrv.im.domain.RoleBean;
import com.vrv.im.domain.UserBean;
import com.vrv.im.domain.UserRoleBean;
import com.vrv.im.persistence.ProjectMapper;
import com.vrv.im.persistence.RoleMapper;
import com.vrv.im.persistence.UserMapper;
import com.vrv.im.persistence.UserRoleMapper;
import com.vrv.im.service.UserRoleService;
import com.vrv.im.vo.BindProjectVo;
import com.vrv.im.vo.ProjectVo;

@Service
public class UserRoleServiceImpl implements UserRoleService {
	@Autowired
	private UserRoleMapper userRoleMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private ProjectMapper projectMapper;
	@Autowired
	private RoleMapper roleMapper;

	public long getRoleIDByUserID(long userID, long projectID) {
		UserRoleBean b = new UserRoleBean();
		b.setUserID(userID);
		b.setProjectID(projectID);
		UserRoleBean roleBean = userRoleMapper.getRoleIDByUserID(b);
		if (roleBean == null)
			return 0;
		else
			return roleBean.getRoleID();
	}

	public long add(BindProjectVo v) {
		UserRoleBean b = new UserRoleBean();
		b.setProjectID(v.getProjectID());
		b.setRoleID(v.getRoleID());
		b.setUserID(v.getUserID());
		return userRoleMapper.addRelation(b);
	}

	public List<ProjectVo> getBindProjectByUserID(long userID) {
		List<UserRoleBean> relations = userRoleMapper.getByUserID(userID);
		if (relations == null || relations.size() == 0)
			return null;
		else {
			List<ProjectVo> results = new ArrayList<ProjectVo>();
			UserBean userBean = userMapper.getUserByUserID(userID);
			for (UserRoleBean b : relations) {
				ProjectVo vo = new ProjectVo();
				vo.setUserID(userBean.getId());
				vo.setUserName(userBean.getName());
				vo.setPhone(userBean.getPhone());
				ProjectBean projectBean = projectMapper.getProjectByID(b.getProjectID());
				if (projectBean != null) {
					vo.setProjectID(projectBean.getProjectID());
					vo.setCommunityName(projectBean.getCommunityName());
					vo.setCommunityNum(projectBean.getCommunityNum());
					vo.setContractNum(projectBean.getContractNum());
					vo.setArea(projectBean.getArea());
					vo.setStatus(projectBean.getStatus());
				}
				RoleBean roleBean = roleMapper.getRoleNameByID(b.getRoleID());
				if (roleBean != null) {
					vo.setRoleID(roleBean.getRoleID());
					vo.setRoleName(roleBean.getRoleName());
				}
				results.add(vo);
			}
			return results;
		}
	}

	public int ubBind(List<UserRoleBean> list) {
		int count = 0;
		for (UserRoleBean b : list) {
			int delCount = userRoleMapper.delRelation(b);
			if (delCount == 1)
				count++;
		}
		return count;
	}

	@Override
	public void addRelation(long projectID, Map<Long, Long> map) {
		if (map != null && map.size() > 0) {
			Set<Entry<Long, Long>> set = map.entrySet();
			Iterator<Entry<Long, Long>> it = set.iterator();
			while (it.hasNext()) {
				Entry<Long, Long> entry = it.next();
				long roleID = entry.getKey();
				long userID = entry.getValue();
				UserRoleBean urb = new UserRoleBean();
				urb.setProjectID(projectID);
				urb.setRoleID(roleID);
				urb.setUserID(userID);
				userRoleMapper.addRelation(urb);
			}
		}
	}

	@Override
	public List<ProjectVo> getAllBindProject() {
		List<ProjectVo> results = new ArrayList<ProjectVo>();
		List<UserRoleBean> relationList = userRoleMapper.getAllRelation();
		if (relationList != null && relationList.size() > 0) {
			for (UserRoleBean b : relationList) {
				ProjectVo v = new ProjectVo();
				UserBean ub = userMapper.getUserByUserID(b.getUserID());
				if (ub != null) {
					v.setUserID(ub.getId());
					v.setUserName(ub.getName());
					v.setPhone(ub.getPhone());
				}
				ProjectBean pb = projectMapper.getProjectByID(b.getProjectID());
				if (pb != null) {
					v.setCommunityName(pb.getCommunityName());
					v.setCommunityNum(pb.getCommunityNum());
					v.setContractNum(pb.getContractNum());
				}
				RoleBean rb = roleMapper.getRoleNameByID(b.getRoleID());
				if (rb != null) {
					v.setRoleID(rb.getRoleID());
					v.setRoleName(rb.getRoleName());
				}
				results.add(v);
			}
		}
		return results;
	}

}
